package com.example.springsecurity.security;

import com.example.springsecurity.domain.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;

/**
 * @ClassName User
 * @Description TODO   实现userDetails  并且当做实时判断的登录信息
 * @Author lvyongwei
 * @Date 2020-08-14 17:09
 **/
@Data
public class User implements UserDetails {

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 权限列表
     */
    private Set<String> permissions;

    /**
     * 用户ip
     */
    private String ip;

    /**
     * 用户地址信息
     */
    private String address;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 浏览器信息
     */
    private String browser;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 用户对象
     */
    private SysUser sysUser;

    public User() {
    }

    public User(Set<String> permissions, SysUser sysUser) {
        this.permissions = permissions;
        this.sysUser = sysUser;
    }

    /**
     * 权限标识
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        /*List<GrantedAuthority> list=new ArrayList<GrantedAuthority>();
        Set<String> set = this.permissions;
        for(String s:set){
            GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(s);
            list.add(grantedAuthority);
        }
        return list;*/
        return null;
    }

    @Override
    public String getPassword() {
        return this.sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return this.sysUser.getUserName();
    }

    /**
     * 账号是否过期
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账号是否被锁定
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 用户凭证是否已过期
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }


    /**
     * 是否能授权
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
